﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace RedRiver.Framework.XtraCommon.FormHelper{
    /// <summary>
    /// FormConst 的摘要说明。
    /// </summary>
    public static class FormConst{
        /// <summary>
        /// 操作菜单 菜单信息
        /// </summary>
        public static readonly MenuItemInfo MenuOperaterInfo =
            new MenuItemInfo("操作", 0, Shortcut.None, MenuMerge.MergeItems, false, false, 'A');


        /// <summary>
        /// 文件菜单 菜单信息
        /// </summary>
        public static readonly MenuItemInfo MenuFileInfo =
            new MenuItemInfo("文件", 10, Shortcut.None, MenuMerge.MergeItems, false, false, 'F');

        /// <summary>
        /// 编辑菜单 菜单信息
        /// </summary>
        public static readonly MenuItemInfo MenuEditInfo =
            new MenuItemInfo("编辑", 20, Shortcut.None, MenuMerge.MergeItems, false, false, 'E');

        /// <summary>
        /// 格式菜单 菜单信息
        /// </summary>
        public static readonly MenuItemInfo MenuFormatInfo =
            new MenuItemInfo("格式", 40, Shortcut.None, MenuMerge.MergeItems, false, false, 'F');

        /// <summary>
        /// 帐簿菜单
        /// </summary>
        public static readonly MenuItemInfo MenuAccountInfo =
            new MenuItemInfo("帐簿", 50, Shortcut.None, MenuMerge.MergeItems, false, false, 'B');

        /// <summary>
        /// 窗口菜单 菜单信息
        /// </summary>
        public static readonly MenuItemInfo MenuWindowInfo =
            new MenuItemInfo("窗口", 60, Shortcut.None, MenuMerge.MergeItems, true, true, 'W');

        /// <summary>
        /// 工具菜单 菜单信息
        /// </summary>
        public static readonly MenuItemInfo MenuToolsInfo =
            new MenuItemInfo("工具", 70, Shortcut.None, MenuMerge.MergeItems, true, true, 'T');

        /// <summary>
        /// 视图菜单 菜单信息
        /// </summary>
        public static readonly MenuItemInfo MenuViewInfo =
            new MenuItemInfo("视图", 80, Shortcut.None, MenuMerge.MergeItems, true, true, 'V');

        /// <summary>
        /// 帮助菜单 菜单信息
        /// </summary>
        public static readonly MenuItemInfo MenuHelpInfo =
            new MenuItemInfo("帮助", 99, Shortcut.None, MenuMerge.MergeItems, false, false, 'H');
    }

    /// <summary>
    /// 菜单信息
    /// </summary>
    public class MenuItemInfo{
        /// <summary>
        /// 通过该值指示是否使用在关联窗体内显示的多文档界面 (MDI) 子窗口列表来填充菜单项。
        /// </summary>
        private readonly bool _mdiList;

        /// <summary>
        /// 通过该值指示菜单项标题
        /// </summary>
        private readonly string _menuName;

        /// <summary>
        /// 通过该值指示菜单项与另一个项合并时的相对位置
        /// </summary>
        private readonly int _mergeOrder;

        /// <summary>
        /// 指定 MenuItem 与另一个菜单中的项进行合并时的行为。
        /// </summary>
        private readonly MenuMerge _mergeType;

        /// <summary>
        /// 通过该值指示与此菜单项关联的助记字符
        /// </summary>
        private readonly char _mnemonic;

        /// <summary>
        /// 通过该值指示 MenuItem（如果已选中）是否显示单选按钮而不是选中标记。
        /// </summary>
        private readonly bool _radioCheck;

        /// <summary>
        /// 通过该值指示与菜单项关联的快捷键。
        /// </summary>
        private readonly Shortcut _shortcut;

        public MenuItemInfo(string menuName,
                            int mergeOrder,
                            Shortcut shortcut,
                            MenuMerge mergeType,
                            bool mdiList,
                            bool radioCheck,
                            char mnemonic){
            _menuName = menuName;
            _mergeOrder = mergeOrder;
            _shortcut = shortcut;
            _mergeType = mergeType;
            _mdiList = mdiList;
            _radioCheck = radioCheck;
            _mnemonic = mnemonic;
        }

        /// <summary>
        /// 通过该值指示与此菜单项关联的助记字符
        /// </summary>
        public char Mnemonic{
            get { return _mnemonic; }
        }

        /// <summary>
        /// 通过该值指示是否使用在关联窗体内显示的多文档界面 (MDI) 子窗口列表来填充菜单项。
        /// </summary>
        public bool MdiList{
            get { return _mdiList; }
        }

        /// <summary>
        /// 指定 MenuItem 与另一个菜单中的项进行合并时的行为。
        /// </summary>
        public MenuMerge MergeType{
            get { return _mergeType; }
        }

        /// <summary>
        /// 通过该值指示与菜单项关联的快捷键。
        /// </summary>
        public Shortcut Shortcut{
            get { return _shortcut; }
        }

        /// <summary>
        /// 通过该值指示菜单项标题
        /// </summary>
        public string MenuName{
            get { return _menuName; }
        }

        /// <summary>
        /// 通过该值指示菜单项与另一个项合并时的相对位置
        /// </summary>
        public int MergeOrder{
            get { return _mergeOrder; }
        }

        /// <summary>
        /// 通过该值指示 MenuItem（如果已选中）是否显示单选按钮而不是选中标记。
        /// </summary>
        public bool RadioCheck{
            get { return _radioCheck; }
        }

        /// <summary>
        ///  根据该<see cref="MenuItemInfo"/>生成的<see cref="MenuItem"/>实例
        /// </summary>
        public MenuItem GetMenuItem(Form form){
            if (form.Menu == null){
                form.Menu = new MainMenu();
            }
            MenuItem menuItem = null;
            foreach (MenuItem item in form.Menu.MenuItems){
                if (item.MergeOrder == MergeOrder){
                    menuItem = item;
                }
            }
            if (menuItem == null){
                menuItem = new MenuItem(string.Concat(_menuName, "(", _mnemonic, ")")){
                                                                                          MergeOrder = MergeOrder,
                                                                                          Index = MergeOrder,
                                                                                          MergeType = MergeType,
                                                                                          Shortcut = Shortcut,
                                                                                          ShowShortcut = true,
                                                                                          MdiList = MdiList,
                                                                                          RadioCheck = RadioCheck
                                                                                      };
            }
            return menuItem;
        }
    }
}